extends layout

block content
  .container

    .card
      h4.card-header Public / Private Key Pairs
      .card-body
        form.form-horizontal
          .form-group
            label.control-label(for='data') Private Key
            .input-group
              input.form-control#privateKey(aria-label='Private Key', type='number')
              span.input-group-btn
                button.btn.btn-secondary#randomButton(type='button', ) Random
          .form-group
            label.control-label(for='data') Public Key
            input#publicKey.form-control(disabled)

  script.
    var EC = elliptic.elliptic().ec;
    var ec = new EC('secp256k1');

    var keypair = ec.genKeyPair();
    if (Cookies.get('privateKey')) {
      keypair = ec.keyFromPrivate(Cookies.get('privateKey'));
    }

    function update() {
      var prv = keypair.getPrivate('hex');
      var pub = keypair.getPublic('hex');
      $('#privateKey').val(bigInt(prv, 16).toString());
      $('#publicKey').val(pub);
      Cookies.set('privateKey', prv.toString());
      Cookies.set('publicKey', pub.toString());
    }

    function random() {
      keypair = ec.genKeyPair();
      update();
    }

    $(function() {
      $('#randomButton').click(random);
      $('#privateKey').bind('keyup', function() {
        $('#privateKey').val($('#privateKey').val().replace(/\D/g, ''));
        keypair = ec.keyFromPrivate(bigInt($('#privateKey').val()).toString(16));
        update();
      });
      update();
    });
